<template>
	<div>
		<el-dialog title="修改管理员" :visible.sync="editorVisible" width="30%" :before-close="closeEditorManager" @open="opendEnd">
			<el-form :model="managerInfo" ref="editorManager" label-width="100px">
			  <el-form-item label="收货人" prop="consigneeName" :rules="[
			        { required: true, message: '请输入收货人姓名', trigger: 'blur' },
			      ]">
			  	<el-input type="text" v-model="managerInfo.consigneeName" placeholder="请输入收货人姓名"
			    clearable></el-input>
			  </el-form-item>

			  <el-form-item label="手机号码" prop="phoneNum" :rules="[
			        { required: true, message: '请输入手机号码', trigger: 'blur' },
			      ]">
			  	<el-input type="text" v-model="managerInfo.phoneNum" placeholder="请输入手机号码"
			    clearable maxlength="11" show-word-limit></el-input>
			  </el-form-item>

			  <el-form-item label="收货地址" prop="address" :rules="[
			        { required: true, message: '请输入收货地址', trigger: 'blur' },
			      ]">
			  	<el-input type="textarea" v-model="managerInfo.address" placeholder="请输入收货地址"
			    clearable maxlength="50" show-word-limit :autosize="{ minRows: 2, maxRows: 5}"></el-input>
			  </el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="closeEditorManager">取 消</el-button>
				<el-button type="primary" @click="editorManager('editorManager')">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name: "",
		props: ["editorVisible","editorObj"],
		data() {
			return {
        managerInfo:{
          consigneeName:"",
          phoneNum:"",
          address:"",
        },
			}
		},
		methods:{
      //清除文本框数据，返回相对应文本框的值
      clearData(){
          this.managerInfo={
            consigneeName:"",
            phoneNum:"",
            address:"",
          },
          this.opendEnd()
      },
      //关闭
			closeEditorManager(){
				// console.log(this)
        this.clearData()
				this.$parent.editorVisible=false
			},
      //需要等待dialog 打开完毕后 在金数据渲染
      opendEnd(){
        this.managerInfo.consigneeId=this.$props.editorObj.consigneeId
        this.managerInfo.consigneeName=this.$props.editorObj.consigneeName
      	this.managerInfo.phoneNum=this.$props.editorObj.phoneNum
      	this.managerInfo.address=this.$props.editorObj.address
      },

      //更新数据
			editorManager(params) {
        var self = this
				this.$refs[params].validate((valid) => {
          if (valid) {
            var manager = JSON.parse(sessionStorage.getItem("manager"))
            // 检测输入的数据中是否存在空格
            var consigneeName = this.managerInfo.consigneeName
            var phoneNum = this.managerInfo.phoneNum
            var address = this.managerInfo.address;
            //防止判断时数据null空值错误
            if(consigneeName==null){
              consigneeName=''
            }
            if(phoneNum==null){
              phoneNum=''
            }
            if(address==null){
              address=''
            }
            //设置手机号正则验证
            var phoneRule = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if(consigneeName.split(" ").length==1){
              if(phoneNum.split(" ").length==1){
                if(phoneRule.test(phoneNum)){
                    self.$http({
                        url:"/api/CakeShop/MemberInfo/updConsignee",
                        method:"post",
                        headers:{
                          token:manager.token
                        },
                        data:{
                          consigneeId:self.managerInfo.consigneeId,
                          consigneeName: self.managerInfo.consigneeName,
                          phoneNum: self.managerInfo.phoneNum,
                          address: self.managerInfo.address,
                        }
                    }).then(function(res){
                      console.log(res)
                      if(res.data.udpSuccNum>0){
                        self.$message({
                          message: res.data.msg,
                          type: 'success',
                          onClose:function(){
                            self.clearData()
                            //关闭修改界面
                            self.closeEditorManager()
                            //根据修改数据返回当前页
                            self.$parent.getMemberList(sessionStorage.getItem("page"))
                          }
                        });
                      }else{
                        self.$message({
                          message: res.data.msg,
                          type: 'error',
                        });
                      }
                    })
                }else{
                  self.$message({
                  	duration: 1500,
                  	message: "手机号码不符合规格!",
                  	type: "error",
                  	onClose: function() {
                  		self.loading = false;
                  	},
                  });
                }
              }else{
                self.$message({
                	duration: 1500,
                	message: "手机号码中存在非法字符空格!",
                	type: "error",
                	onClose: function() {
                		self.loading = false;
                	},
                });
              }
            }else{
              self.$message({
              	duration: 1500,
              	message: "收货人姓名中存在非法字符空格!",
              	type: "error",
              	onClose: function() {
              		self.loading = false;
              	},
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
				});
			}
		}

	}
</script>

<style>
</style>
